let Home = {
    baseUrl: 'http://81.71.65.4:3003',
    renderGwc(token) {
        let html = ''
        axios.post(this.baseUrl + '/cart/all', { token: token }).then((res) => {
            // console.log(res);
            let data = res.data.list
            data.forEach((v, i) => {
                // console.log(v);
                html += `<div class="items" cid=${v.cartId}>
                <div class="check">
                    <input type="checkbox" class="cebtn" id="checks">
                </div>
                <div class="it-img" >
                    <a href="./xiangqing.html?sid=${v.productId}"> 
                    <img src="${v.imgUrl}"></a>
                </div>
                <a href="#?sid=${v.productId}">
                    <div class="it-xx" cid=${v.cartId}>
                       <div class="it-xx2">
                        <p>${v.masterName}</p>
                        <button>删除</button>
                       </div>
                        <div class="xx-num">
                            <div class="xx-num1">￥
                                <span class="xx-num1-sp">${v.price/100}</span>
                            </div>
                            <div class="xx-num2">X
                                <input type="number" name="" id="" value="${v.buyNum}"  min="1">
                            </div>
                        </div>
                    </div>
                </a>

            </div>`
            })

            document.querySelector('#main').innerHTML = html
            this.renderPrice()
            this.singleCheck()
            this.numberClick()
            this.deleShop()

        }).catch(() => {
            console.log('错误');
        })
    },
    /**
     * 全选按钮
     */
    AllCheck() {
        let all = document.querySelector('#allche')
        all.addEventListener('click', () => {
            let state = document.querySelectorAll('#main  .check [type=checkbox]')
            state.forEach((v, i) => {
                v.checked = all.checked
                if (all.checked) {
                    this.renderPrice()
                } else {
                    document.querySelector('.js-num .sp3').innerText = 0
                }

            })
        })
    },

    /**
     * 单个点击按钮
     */
    singleCheck() {
        let all = document.querySelector('#allche')
        document.querySelectorAll('#checks').forEach((v, i) => {
            // console.log(v);
            v.onclick = (e) => {
                let sta = e.target.checked
                console.log(sta, all.checked);
                if (!sta) {
                    all.checked = false
                } else {
                    all.checked = document.querySelectorAll('#checks').length == document.querySelectorAll('#checks:checked').length
                }
                this.renderPrice()
            }
        })
    },
    /**
     * 小计
     */
    renderPrice() {
        let zong = 0
        document.querySelectorAll('.items').forEach((v, i) => {
            // console.log(v);
            let status = v.querySelector('[type=checkbox]').checked
                // console.log(status);
            if (status) {
                let price = v.querySelector('.xx-num1-sp').innerText
                let num = v.querySelector('[type=number]').value
                    // console.log(num, price);
                let mt = +price * +num
                zong += mt
                console.log(zong);
            }
        })
        document.querySelector('.js-num .sp3').innerText = zong
    },
    /**
     * 数量点击
     */
    numberClick() {
        let state = document.querySelectorAll('#main .items [type=number]')
        for (let i = 0; i < state.length; i++) {
            state[i].addEventListener('change', () => {
                this.renderPrice()
            })
        }
    },

    /**
     * 删除商品
     */
    deleShop() {
        let token = localStorage.getItem('token')
        let btn = document.querySelectorAll('.it-xx2 button')
        let cid = []
        for (let i = 0; i < btn.length; i++) {
            btn[i].addEventListener('click', (e) => {
                // console.log(e.target.parentNode.parentNode.getAttribute('cid'));
                cid.push(e.target.parentNode.parentNode.getAttribute('cid'))
                    // console.log(cid);
                axios.post(this.baseUrl + '/cart/del', { token: token, cartId: cid }).then((res) => {
                    if (res.status == 200) {
                        console.log('删除成功');
                        this.renderGwc(token)
                        renderCart()
                    }
                })
            })
        }
    }
}

onload = () => {
    let token = localStorage.getItem('token')
        // console.log(token);
    Home.renderGwc(token)
    Home.AllCheck()

}